package cuiyt.datastructure.avltree;

import cuiyt.datastructure.searchtree.BinarySearchTree;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author cyt
 * @describe
 * @create 2020-12-17 20:20
 */

public class SearchTree<E> implements BinarySearchTree<E> {

    private int size;
    private Node<E> rootNode;
    private Comparator<E> comparator;

    /**
     * 添加node之后的调整
     *
     * @param node 新添加的节点
     */
    protected void afterAdd(Node<E> node) {
    }

    /**
     * 新建一个节点
     *
     * @param element
     * @param parent
     * @return
     */
    protected Node<E> createNode(E element, Node<E> parent) {
        return new Node<>(element, parent);
    }

    public SearchTree() {
        this(null);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        toString(rootNode, sb, "");
        return sb.toString();
    }

    private void toString(Node root, StringBuilder sb, String preFix) {
        if (root == null) {
            return;
        }
        sb.append(preFix).append(root.value).append("\n");
        toString(root.left, sb, preFix + "L");
        toString(root.right, sb, preFix + "R");
    }


    public static abstract class Visitor<E> {
        boolean isStop;

        /**
         * 具体的打印方法
         *
         * @param e
         * @return
         */
        protected abstract boolean visitor(E e);
    }

    public void levelOrder(Visitor visitor) {
        System.out.println("\nlevelOrder - > ");
        levelOrderMethod(rootNode, visitor);
    }

    private void levelOrderMethod(Node node, Visitor visitor) {
        if (node == null || visitor == null) {
            return;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.add(node);
        while (!queue.isEmpty()) {
            Node remove = queue.remove();
            //System.out.println(remove.value);
            if (visitor.visitor(remove.value)) {
                return;
            }
            if (remove.left != null) {
                queue.add(remove.left);
            }
            if (remove.right != null) {
                queue.add(remove.right);
            }
        }
    }

    public void postOrder(Visitor visitor) {
        System.out.println("\npostOrder - > ");
        if (visitor == null) {
            return;
        }
        postOrderMethod(rootNode, visitor);
    }

    private void postOrderMethod(Node node, Visitor visitor) {
        if (node == null) {
            return;
        }
        postOrderMethod(node.left, visitor);
        postOrderMethod(node.right, visitor);
        if (visitor.isStop) {
            return;
        }
        visitor.isStop = visitor.visitor(node.value);
    }

    public void inOrder() {
        System.out.println("\ninOrder - > ");
        inOrderMethod(rootNode);
    }

    private void inOrderMethod(Node node) {
        if (node != null) {
            inOrderMethod(node.left);
            System.out.println(node.value);
            inOrderMethod(node.right);
        }
    }

    public void preOrder() {
        System.out.println("\npreOrder - > ");
        preOrderMethod(rootNode);
    }

    private void preOrderMethod(Node node) {
        if (node != null) {
            System.out.println(node.value);
            preOrderMethod(node.left);
            preOrderMethod(node.right);
        }
    }

    /**
     * 提供外部比较器
     *
     * @param comparator
     */
    public SearchTree(Comparator comparator) {
        this.comparator = comparator;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void clear() {

    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void add(E element) {
        checkElement(element);
        /**
         * 添加根节点
         */
        if (rootNode == null) {
            rootNode = createNode(element, null);
            // rootNode = new Node<>(element, null);
            size++;
            afterAdd(rootNode);
            return;
        }
        /**
         * 添加 非根节点
         * 1、找到根节点
         * 2、对比左右子树，找到要插入的位置
         *
         * res 当前要插入位置的方向
         * parent 当前要插入节点的父节点
         *
         * */
        Node<E> node = rootNode;
        Node<E> parent = null;
        int res = 0;
        while (node != null) {
            res = compare(element, node.value);
            parent = node;
            if (res > 0) {
                node = node.right;
            } else if (res < 0) {
                node = node.left;
            } else {
                /**
                 * 相等直接就返回了
                 */
                node.value = element;
                return;
            }
        }
        Node<E> newNode = createNode(element, parent);
        /* Node<E> newNode = new Node<>(element, parent);*/
        /**
         * 找到父节点之后， 开始插入元素
         */
        if (res > 0) {
            parent.right = newNode;
        } else if (res < 0) {
            parent.left = newNode;
        }
        size++;
        afterAdd(newNode);
    }

    /**
     * 比较节点大小的函数
     *
     * @param e1
     * @param e2
     * @return
     */
    private int compare(E e1, E e2) {
        /**
         * 如果提供的有外部比较器就使用外部比较器
         * 没有的话， 就直接使用元素本身的比较策略来比较
         */
        if (comparator != null) {
            return comparator.compare(e1, e2);
        } else {
            /**
             * 强制转化为  Comparable 进行元素的比较
             */
            return ((Comparable<E>) e1).compareTo(e2);
        }
    }

    @Override
    public void remove(E element) {

    }

    @Override
    public boolean contains(E element) {
        return false;
    }

    /**
     * 检查要插入的元素
     *
     * @param e
     */
    private void checkElement(E e) {
        if (e == null) {
            throw new IllegalArgumentException("element must is not null");
        }
    }

    @Override
    public Object root() {
        return rootNode;
    }

    @Override
    public Object left(Object node) {
        return ((Node) node).left;
    }

    @Override
    public Object right(Object node) {
        return ((Node) node).right;
    }

    @Override
    public Object string(Object node) {
        Node node1 = (Node) node;
        String patentString = "null";
        if (node1.parent != null) {
            patentString = node1.parent.value.toString();
        }
        return node1.value;
    }

    /**
     * 内部维护的节点， 使用节点来组成一个树
     *
     * @param <E>
     */
    protected static class Node<E> {
        E value;
        Node<E> left;
        Node<E> right;
        Node<E> parent;

        public boolean isLeft() {
            return parent != null && this == parent.left;
        }

        public boolean isRight() {
            return parent != null && this == parent.right;
        }

        Node(E element, Node<E> parent) {
            this.value = element;
            this.parent = parent;
        }
    }

}
